const { DataTypes, Model } = require('sequelize')
const { generatId, formatReturnId } = require('@/utils/idUtil')
const Mysequelize = require('./index')
const baseModel = require('./baseModel')

class Source extends Model {}
Source.init(
  {
    id: {
      type: DataTypes.BIGINT,
      defaultValue: generatId(),
      primaryKey: true,
      allowNull: false,
      get() {
        return formatReturnId(this.getDataValue('id'))
      },
      set(value) {
        this.setDataValue('id', BigInt(value))
      }
    },
    desc: {
      type: DataTypes.STRING(255)
    },
    targetId: {
      type: DataTypes.BIGINT,
      allowNull: false,
      field: 'target_id',
      get() {
        return formatReturnId(this.getDataValue('targetId'))
      },
      set(value) {
        this.setDataValue('targetId', BigInt(value))
      }
    },
    fileId: {
      type: DataTypes.BIGINT,
      allowNull: false,
      field: 'file_id',
      get() {
        return formatReturnId(this.getDataValue('fileId'))
      },
      set(value) {
        this.setDataValue('fileId', BigInt(value))
      }
    },
    parentId: {
      type: DataTypes.BIGINT,
      allowNull: false,
      field: 'parent_id',
      get() {
        return formatReturnId(this.getDataValue('parentId'))
      },
      set(value) {
        this.setDataValue('parentId', BigInt(value))
      }
    },
    parentLevel: {
      type: DataTypes.TEXT,
      field: 'parent_level'
    },
    isDir: {
      type: DataTypes.TINYINT(1),
      allowNull: false,
      field: 'is_dir'
    },
    type: {
      type: DataTypes.TINYINT(4),
      allowNull: false
    },
    filename: {
      type: DataTypes.STRING(255),
      allowNull: false
    },
    sizeDesc: {
      type: DataTypes.STRING(255),
      allowNull: false,
      field: 'size_desc'
    },
    deleteFlag: {
      type: DataTypes.TINYINT(1),
      allowNull: false,
      field: 'delete_flag'
    },
    targetType: {
      type: DataTypes.TINYINT(1),
      allowNull: false,
      field: 'target_type'
    },
    viewTime: {
      type: DataTypes.DATE,
      defaultValue: DataTypes.NOW,
      field: 'view_time'
    },
    ...baseModel
  },
  {
    // 这是其他模型参数
    sequelize: Mysequelize, // 我们需要传递连接实例
    modelName: 'Source', // 我们需要选择模型名称
    tableName: 'source', // 数据库表名称
    createdAt: 'createTime',
    // 想要 updatedAt 但是希望名称叫做 updateTime
    updatedAt: 'updateTime'
  }
)

// 定义的模型是类本身
// console.log(Source === sequelize.models.Source); // true
module.exports = Source
